from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response
import rest_framework.status as status
from django.views.decorators.http import require_http_methods
from projectManage.models import Project
from django.utils import timezone

class ProjectCreate(APIView):
    permission_classes = [IsAuthenticated]
    def post(self, request):
        try:
            user_id=request.user.id
            title=request.data['title']
            introduction=request.data['introduction']
            modify_time = timezone.now()
            Project.objects.create(user_id=user_id,title=title,introduction=introduction,create_time=modify_time,modify_time=modify_time,type=1,isCollect=False,isDelete=False)
            new_project_id = Project.objects.filter(user_id=user_id).last().project_id
            # Project.objects.filter(user_id=user_id).delete()
            return Response({
                "message": "success",
                "project_id": new_project_id
            }, status=status.HTTP_201_CREATED)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)

class GetProjects(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        try:
            user_id=request.user.id
            showType=request.GET.get('showType')
            projects=[]
            if showType=='All':
                projects=Project.objects.filter(user_id=user_id,isDelete=False).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            elif showType=='collect':
                projects=Project.objects.filter(user_id=user_id,isCollect=True,isDelete=False).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            elif showType=='delete':
                projects=Project.objects.filter(user_id=user_id,isDelete=True).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            return Response({
                "projects": list(projects)
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)

class ProjectCollect(APIView):
    permission_classes = [IsAuthenticated]
    def put(self, request):
        try:
            user_id=request.user.id
            project_id=request.data['project_id']
            project = Project.objects.get(user_id=user_id,project_id=project_id)
            project.isCollect = not project.isCollect
            project.save()
            return Response({
                "message": "success"
            }, status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)

class ProjectDelete(APIView):
    permission_classes = [IsAuthenticated]
    def post(self, request):
        try:
            user_id=request.user.id
            project_id=request.data['project_id']
            delOrRecover=request.data['delOrRecover']
            if delOrRecover=='strikeout':
                Project.objects.filter(user_id=user_id,project_id=project_id).delete()
            else:
                project = Project.objects.get(user_id=user_id,project_id=project_id)
                project.isDelete=not project.isDelete
                project.save()
            return Response({
                "message": "success"
            }, status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)
        
class ProjectModify(APIView):
    permission_classes = [IsAuthenticated]
    def put(self, request):
        try:
            user_id=request.user.id
            project_id=request.data['project_id']
            title=request.data['title']
            introduction=request.data['introduction']
            project = Project.objects.get(user_id=user_id,project_id=project_id)
            project.title=title
            project.introduction=introduction
            project.modify_time = timezone.now()
            project.save()
            return Response({
                "message": "success"
            }, status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)
        
class ProjectSearch(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        try:
            user_id=request.user.id
            searchContent=request.GET.get('searchContent')
            searchType=request.GET.get('searchType')
            projects=[]
            if searchType=='Home':
                projects=Project.objects.filter(user_id=user_id,isDelete=False,title__contains=searchContent).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            elif searchType=='Starred':
                projects=Project.objects.filter(user_id=user_id,isCollect=True,isDelete=False,title__contains=searchContent).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            elif searchType=='Trash':
                projects=Project.objects.filter(user_id=user_id,isDelete=True,title__contains=searchContent).values('project_id','title', 'introduction', 'create_time', 'modify_time','isCollect','isDelete').order_by('-modify_time')
            return Response({
                "projects": list(projects)
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                "message": str(e)
            }, status.HTTP_400_BAD_REQUEST)
        